import verifys from 'service/util/verifys.js'
// import util from 'service/util'

export default {
    data() {
        return {
            mobile: '',
            password: '',
            fromName: null
        }
    },
    beforeRouteEnter: (to, from, next) => {
        next(vm => {
            if (to.params) {
                if (to.params.redirect) {
                    vm.fromName = from.name
                } else if (to.params.redirectName) {
                    vm.fromName = to.params.redirectName
                }
            }
        })
    },
    methods: {
        loginDo() {
            if (verifys.verify(['mobile', 'password'], this) == false) {
                return
            }
            $loading.show()
            var { mobile, password } = this
            $ajax.account.login({ mobile, password }).then(res => {
                var { code, msg, data } = res.data
                $toast.show(msg)
                if (code == 'S') {
                    $store.dispatch('userUpdate', data)
                    $wxsdk.postLbs({
                        scene: 2,
                        mobile: $store.state.user.mobile,
                        userId: $store.state.user.userId
                    })
                    // 白骑士指纹设备信息上传-暂时不做
                    // $store.dispatch('userConfitAjax')
                    if (this.fromName) {
                        $router.replace({
                            name: this.fromName
                        })
                        return
                    }
                    if ($store.getters.userBorrowStatus == 'Y') {
                        $router.replace('/loan/info')
                    } else {
                        $router.replace('/loan/action')
                    }
                }
            }).catch(err => {
                $toast.show('网络异常')
            })
        }
    }
}